package com.example.zzh.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zzh.entity.ZzhOrganization;
import com.example.zzh.entity.ZzhProductUnit;
import com.example.zzh.entity.ZzhRole;
import com.example.zzh.entity.ZzhStaff;
import com.example.zzh.service.ZzhProductUnitService;
import com.example.zzh.utils.JwtTokenUtils;
import com.example.zzh.utils.Result;
import com.example.zzh.utils.ResultGenerator;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 多单位表 前端控制器
 * </p>
 *
 * @author zzh
 * @since 2023-08-12
 */
@RestController
@RequestMapping("/zzh-product-unit")
public class ZzhProductUnitController {

    @Autowired
    private ZzhProductUnitService zzhProductUnitService;

    @GetMapping("/list")
    @ApiOperation(value = "查询所有计量单位", notes = "查询所有计量单位")
    public Result list(){
        return ResultGenerator.genSuccessResult(zzhProductUnitService.list());
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询所有计量单位分页", notes = "分页")
    public Result list(@RequestBody Map<String,Object> params){
        if (StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        Page<ZzhProductUnit> page=new Page<>();
        page.setCurrent((int) params.get("page"));
        page.setSize((int) params.get("limit"));
        LambdaQueryWrapper<ZzhProductUnit> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        IPage r=null;
        if(!StringUtils.isEmpty(params.get("name"))){
            lambdaQueryWrapper.like(ZzhProductUnit::getName,params.get("name"));
        }
        lambdaQueryWrapper.in(ZzhProductUnit::getStatus,1,2);
        r=zzhProductUnitService.pageNames(page,lambdaQueryWrapper);
        return ResultGenerator.genSuccessResult(r.getRecords(),r.getTotal());
    }

    @PutMapping("/updateStatus")
    @ApiOperation(value = "修改计量单位状态", notes = "修改计量单位状态")
    public Result updateStatus(@RequestParam Long id){
        ZzhProductUnit zzhProductUnit=zzhProductUnitService.getProductById(id);
        if(!Objects.isNull(zzhProductUnit)){
            if(zzhProductUnit.getStatus()==1){
                zzhProductUnit.setStatus(2);
            }else if(zzhProductUnit.getStatus()==2){
                zzhProductUnit.setStatus(1);
            }
            if(zzhProductUnitService.updateById(zzhProductUnit)) {
                return ResultGenerator.genSuccessResult();
            }
            return ResultGenerator.genErrorResult(404,"更新不成功");
        }else {
            return ResultGenerator.genErrorResult(404,"更新不成功");
        }
    }


    @PostMapping("/add")
    @ApiOperation(value = "新增计量单位", notes = "新增计量单位信息")
    public Result add(@RequestBody ZzhProductUnit zzhProductUnit){
        if(zzhProductUnitService.save(zzhProductUnit)){
            return ResultGenerator.genSuccessResult();
        }else {
            return ResultGenerator.genErrorResult(404,"新增不成功");
        }
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改计量单位", notes = "修改计量单位")
    public Result mod(@RequestBody ZzhProductUnit zzhProductUnit){
        if(zzhProductUnitService.updateById(zzhProductUnit)){
            return ResultGenerator.genSuccessResult();
        }else {
            return ResultGenerator.genErrorResult(404,"修改不成功");
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除计量单位", notes = "删除计量单位信息")
    public Result delete(@RequestParam Long id){
        ZzhProductUnit zzhProductUnit=zzhProductUnitService.getProductById(id);
        if(!Objects.isNull(zzhProductUnit)){
            zzhProductUnit.setStatus(0);
            if(zzhProductUnitService.updateById(zzhProductUnit)) {
                return ResultGenerator.genSuccessResult();
            }
            return ResultGenerator.genErrorResult(404,"删除不成功");
        }else {
            return ResultGenerator.genErrorResult(404,"删除不成功");
        }
    }

}
